const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            Level: [
                { label: '省', value: '1' },
                { label: '市', value: '2' },
                { label: '区/县', value: '3' },
                { label: '街道/乡镇', value: '4' },
                { label: '社区/村庄', value: '5' }
            ],
            dataForm:{
                Row_Status:'2'
            },
            village: [],
            cityValue: [],
            village1: [],
            cityValue1:[]
        };
    },
    mounted() {
        this.getCategory();
        let ls_URL = window.location.href;
        let ProvinceNumber = getQueryObject(ls_URL, "ProvinceNumber");   // 获取 action 参数
        if (ProvinceNumber) {
            if (ProvinceNumber.length == 6) {
                if (/^0{4}$/.test(ProvinceNumber.slice(-4))) {//省
                    this.cityValue = [ProvinceNumber];
                } else if (/^0{2}$/.test(ProvinceNumber.slice(-2))) {//市
                    this.cityValue = [ProvinceNumber.substring(0, 2)+'0000' , ProvinceNumber];
                } else {//区
                    this.cityValue = [ProvinceNumber.substring(0, 2)+'0000',ProvinceNumber.substring(0, 4)+'00',ProvinceNumber];
                    this.changeValue(this.cityValue);
                }
            } else {
                this.cityValue = [ProvinceNumber.substring(0, 2)+'0000',ProvinceNumber.substring(0, 4)+'00',ProvinceNumber.substring(0, 6)];
                this.changeValue(this.cityValue);
                if (ProvinceNumber.length == 9) {//街道
                    this.cityValue1 = [ProvinceNumber];
                } else if (ProvinceNumber.length == 12) {//小区
                    this.cityValue1 = [ProvinceNumber.substring(0, 9),ProvinceNumber];
                }
                this.changeValue1(this.cityValue1);
            } 
        } else if(this.action=='Add') {
            this.cityValue = ["230000", "232700", "232723"];
            this.changeValue(this.cityValue)
        }
    },
    watch: {
        'dataForm.District_Introduction_SerialNumber'(val){
            console.log(this.dataForm.District_Number);
            let str = this.dataForm.District_Number;
            if (this.dataForm.District_Number.length == 6) {
                if (str.includes('0000')) {
                    this.cityValue = [str];
                } else if (str.includes('00')) {
                    this.cityValue = [str.substring(0,2)+'0000',str];
                } else {
                    this.cityValue = [str.substring(0,2)+'0000',str.substring(0,4)+'00',str.substring(0,6)];
                }
            } else if (this.dataForm.District_Number.length == 9) {
                this.cityValue = [str.substring(0,2)+'0000',str.substring(0,4)+'00',str.substring(0,6)];
                this.changeValue(this.cityValue);
                this.cityValue1 = [str.substring(0,9)];
            } else if (this.dataForm.District_Number.length == 12) {
                this.cityValue = [str.substring(0,2)+'0000',str.substring(0,4)+'00',str.substring(0,6)];
                this.changeValue(this.cityValue);
                this.cityValue1 = [str.substring(0,9),str];
            }
        }
    },
    methods: {
        validator(message, value, callback){
            if (!value) {
                callback(new Error(message));
            } else if (isNaN(value)) {
                callback(new Error('请输入正确的面积'));
            } else {
                callback();
            }
        },
        changeValue(val) {
            this.dataForm.District_Number = val[val.length - 1];
            this.cityValue1 = [];
            if (val.length == 3) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/clientOperation/getDistrictTree`,
                    type: "GET",
                    data: {
                      DistrictCode: val[2],
                    },
                    success: (data) => {
                      this.village1 = data[0].children;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        },
        changeValue1(val){
            if (val&&val.length > 0) {
                this.dataForm.District_Number = val[val.length - 1];
            } else {
                this.dataForm.District_Number = this.cityValue[this.cityValue.length - 1];
            }
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'news_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.category = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                url: `${absolutepath()}api/Districtjl.json`,
                method: "get",
                success: (data) => {
                    data.forEach(item => {
                        item.children.forEach(ite => {
                            ite.children.forEach(it => {
                                it.children = undefined
                            })
                        })
                    })
                    this.village = data;
                }
            });
        }
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader)
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
